package com.dq.demo.ui.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dq.demo.util.StatusBarUtil;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author Administrator
 * @particulars
 * @time 2019\10\24 0024 9:56
 * @class describe
 */
public abstract class BaseFragment extends Fragment {

    public String TAG = getClass().getSimpleName();
    public Activity activity;
    public int height;
    public View mView;
    public Unbinder mUnBinder;

    /**
     * Fragment当前状态是否可见
     **/
    public boolean isVisible = false;

    /**
     * Fragment的view是否已创建
     **/
    public boolean mIsViewCreated = false;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = getActivity();
        height = StatusBarUtil.getStatusBarHeight(activity);
    }

    /**
     * Activity跳转
     *
     * @param startAct
     * @param endAct
     * @return
     */
    public void startAct(Activity startAct, Activity endAct) {
        Intent intent = new Intent(startAct, endAct.getClass());
        startActivity(intent);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(getLayoutId(), null);
        mIsViewCreated = true;
        onVisible();
        return mView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mUnBinder = ButterKnife.bind(this, view);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnBinder.unbind();
        mIsViewCreated = false;
        beforeDestroy();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (!mIsViewCreated) {
            return;
        }
        if (isVisibleToUser) {
            onVisible();
        } else {
            onHidden();
        }
    }

    public void onVisible() {
    }

    public void onHidden() {
    }

    /**
     * 设置状态栏字体颜色
     *
     * @param colors 颜色值
     */
    public void setStatusBarColor(int colors) {
        if (!StatusBarUtil.setStatusBarDarkTheme(activity, true)) {
            StatusBarUtil.setStatusBarColor(activity, colors);
        }
    }

    protected void setStatusBar() {
        height = StatusBarUtil.getStatusBarHeight(activity);
        //当FitsSystemWindows设置 true 时，会在屏幕最上方预留出状态栏高度的 padding
        StatusBarUtil.setRootViewFitsSystemWindows(activity, true);
        //设置状态栏透明
        StatusBarUtil.setTranslucentStatus(activity);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            onVisible();
            isVisible = true;
        } else {
            onHidden();
            isVisible = false;
        }
    }

    public abstract int getLayoutId();

    public abstract void initData();

    public abstract void initView();

    public abstract void beforeDestroy();

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}